package com.ryder.auth.application.controller;

import com.alibaba.fastjson2.JSON;
import com.google.common.base.Preconditions;
import com.ryder.auth.application.convert.AuthRoleDTOConvert;
import com.ryder.auth.application.dto.AuthRoleDTO;
import com.ryder.auth.common.entity.Result;
import com.ryder.auth.domain.entity.AuthRoleBO;
import com.ryder.auth.domain.service.AuthRoleDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 描述: 角色 controller
 *
 * @author Ryder
 * @version 1.0
 * @since 1.0 // 版本号，指明从哪个版本开始
 * 更新日期: 2025/02/17
 */
@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {

    @Autowired
    private AuthRoleDomainService authRoleDomainService;

    /**
     * 角色添加
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            checkRoleInfo(authRoleDTO);

            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTANCE.convertDtoToBo(authRoleDTO);
            return Result.ok(authRoleDomainService.add(authRoleBO));
        } catch(Exception e) {
            log.error("RoleController.add.error: {}", e.getMessage(), e);
            return Result.fail("角色添加失败");
        }
    }

    /**
     * 更新角色信息
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");
            checkRoleInfo(authRoleDTO);

            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTANCE.convertDtoToBo(authRoleDTO);
            return Result.ok(authRoleDomainService.update(authRoleBO));
        } catch(Exception e) {
            log.error("RoleController.update.error: {}", e.getMessage(), e);
            return Result.fail("更新角色信息失败");
        }
    }

    /**
     * 删除角色
     */
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {
        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
            }
            Preconditions.checkNotNull(authRoleDTO.getId(), "角色id不能为空");

            AuthRoleBO authRoleBO = AuthRoleDTOConvert.INSTANCE.convertDtoToBo(authRoleDTO);
            return Result.ok(authRoleDomainService.delete(authRoleBO));
        } catch(Exception e) {
            log.error("RoleController.delete.error: {}", e.getMessage(), e);
            return Result.fail("删除角色信息失败");
        }
    }

    private void checkRoleInfo(@RequestBody AuthRoleDTO authRoleDTO) {
        Preconditions.checkArgument(StringUtils.isNotBlank(authRoleDTO.getRoleName()), "角色名称不能为空");
        Preconditions.checkArgument(StringUtils.isNotBlank(authRoleDTO.getRoleKey()), "角色唯一标识不能为空");
    }
}
